/*
 * @Author: HeAo
 * @Date: 2021-10-15 17:01:15
 * @LastEditTime: 2021-10-15 17:31:20
 * @LastEditors: HeAo
 * @Description:
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day07\7-axios封装.js
 * 别乱动！
 */
// 1.设置全局默认配置项
axios.defaults.baseURL = 'http://47.94.46.113:8888'

// 2.设置请求拦截器
axios.interceptors.request.use(config => {
  // Do something before request is sent
  let token = sessionStorage.getItem('token')
  if (config.url != '/user/login' && token) {
    config.headers['Authorization'] = token
  }
  return config;
}, error => {
  // Do something with request error
  return Promise.reject(error);
});
// 3.设置响应拦截器
axios.interceptors.response.use(response => {
  // Do something before response is sent
  response = {
    ...response,
    data: response.data.data,
    status: response.data.status,
    statusText: response.data.message
  }
  return response;
}, error => {
  // Do something with response error
  return Promise.reject(error);
});

let qs = Qs
// 4.封装请求方法

function post_form (url, data) {
  return axios({
    url,
    method: 'post',
    data: qs.stringify(data),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
    }
  })
}
